package com.zz.controller;

import java.util.Optional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.Product;
import com.zz.entity.Warning;
import com.zz.service.ProductService;
import com.zz.service.WarningService;
@RestController
@RequestMapping("warning")
public class WarningController {
	@Resource
	WarningService warningService;
	
	@Resource
	ProductService productService;
	
	@RequestMapping("showWarninglist")
	public Page<Object[]> showWarninglist(HttpServletRequest request){
		String start=request.getParameter("start");
		return warningService.showWarninglist(start, "5");
	}
	@RequestMapping("showWarninglistByProName")
	public Page<Object[]> showWarninglistByProName(HttpServletRequest request){
		String productname=request.getParameter("selectname");
		String start=request.getParameter("start");
		return warningService.showWarninglistByProName(productname, start, "5");
	}
	
	@RequestMapping("savewarning")
	public Warning savewarning(HttpServletRequest request) {
		String productid=request.getParameter("productid");
		String minnum=request.getParameter("minnum");
		String maxnum=request.getParameter("maxnum");
		Optional<Product> product=productService.findproductById(productid);
		int num=product.get().getNum();
		int min=Integer.parseInt(minnum);
		int max=Integer.parseInt(maxnum);
		Warning warning=new Warning();
		if(num<min) {
			warning.setStatus("不足");
		}else if(num>max){
			warning.setStatus("积压");
		}else {
			warning.setStatus("正常");
		}
		warning.setProductId(productid);
		warning.setMaxnum(max);
		warning.setMinnum(min);
		return warningService.savewarning(warning);
	}
}
